package com.linx.commonlib

import android.app.Activity
import android.content.Context
import android.util.DisplayMetrics
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import com.linx.commonlib.util.Utils

const val MATCH_PARENT = ViewGroup.LayoutParams.MATCH_PARENT
const val WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT

fun dp2pxf(dp: Float): Float {
    return Utils.app.dp2pxf(dp)
}

fun dp2px(dp: Float): Int {
    return Utils.app.dp2px(dp)
}

fun realScreenWidth(): Int {
    return Utils.app.realScreenWidth()
}

fun realScreenHeight(): Int {
    return Utils.app.realScreenHeight()
}

fun screenWidth(): Int {
    return Utils.app.screenWidth()
}

fun screenHeight(): Int {
    return Utils.app.screenHeight()
}

fun statusBarHeight(): Int {
    return Utils.app.statusBarHeight()
}

fun Context.statusBarHeight(): Int {
    val res = resources.getIdentifier("status_bar_height", "dimen", "android")
    return if (res > 0) {
        resources.getDimensionPixelSize(res)
    } else {
        0
    }
}

fun Context.realScreenWidth(): Int {
    val displayMetrics = DisplayMetrics()
    val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
    windowManager.defaultDisplay.getRealMetrics(displayMetrics)
    return displayMetrics.widthPixels
}

fun Context.realScreenHeight(): Int {
    val displayMetrics = DisplayMetrics()
    val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
    windowManager.defaultDisplay.getRealMetrics(displayMetrics)
    return displayMetrics.heightPixels
}

fun Context.screenWidth(): Int {
    return resources.displayMetrics.widthPixels
}

fun Context.screenHeight(): Int {
    return resources.displayMetrics.heightPixels
}

fun Context.dp2pxf(dp: Float): Float {
    val scale = resources.displayMetrics.density
    return dp * scale
}

fun Context.dp2px(dp: Float): Int {
    val scale = resources.displayMetrics.density
    return (dp * scale + 0.5f).toInt()
}

fun Context.navigationBarHeight(): Int {
    if (this is Activity) {
        if (!isNavigationBarExist()) {
            return 0
        }
    }
    val id = resources.getIdentifier("navigation_bar_height", "dimen", "android")
    return resources.getDimensionPixelOffset(id)
}